﻿using Assimp;
using Assimp.Configs;
using Painter3D.RenderSys.RenderObject;
using System;

namespace Painter3D.RenderSys.RenderObject
{
    class ModuleImporter
    {
        public static bool Load(string path, PostProcessSteps ppsteps, out float[] position, out uint[] index/*, params PropertyConfig[] configs*/)
        {
            AssimpContext importer = new AssimpContext();
            //importer.SetConfig(config);
            Scene scene = importer.ImportFile(path, ppsteps);
            if (scene != null && scene.MeshCount != 0)
            {
                Mesh mesh = scene.Meshes[0];
                position = new float[mesh.Vertices.Count * 3];
                index = new uint[mesh.FaceCount * 3];
                for (int i = 0; i < mesh.Vertices.Count; i++)
                {
                    position[i * 3] = mesh.Vertices[i].X;
                    position[i * 3 + 1] = mesh.Vertices[i].Y;
                    position[i * 3 + 2] = mesh.Vertices[i].Z;
                }
                for (int i = 0; i < mesh.FaceCount; i++)
                {
                    index[i * 3] = (uint)mesh.Faces[i].Indices[0];
                    index[i * 3 + 1] = (uint)mesh.Faces[i].Indices[1];
                    index[i * 3 + 2] = (uint)mesh.Faces[i].Indices[2];
                }
                return true;
            }
            position = null;
            index = null;
            return false;
        }

        public static bool Load(string path, PostProcessSteps ppsteps, out float[] position, out uint[] index, out float[] normal/*, params PropertyConfig[] configs*/)
        {
            AssimpContext importer = new AssimpContext();
            //importer.SetConfig(config);
            Scene scene = importer.ImportFile(path, ppsteps);
            if (scene != null && scene.MeshCount != 0)
            {
                Mesh mesh = scene.Meshes[0];
                position = new float[mesh.Vertices.Count * 3];
                index = new uint[mesh.FaceCount * 3];
                normal = new float[mesh.Normals.Count * 3];
                for (int i = 0; i < mesh.Vertices.Count; i++)
                {
                    position[i * 3] = mesh.Vertices[i].X;
                    position[i * 3 + 1] = mesh.Vertices[i].Y;
                    position[i * 3 + 2] = mesh.Vertices[i].Z;
                }
                for (int i = 0; i < mesh.FaceCount; i++)
                {
                    index[i * 3] = (uint)mesh.Faces[i].Indices[0];
                    index[i * 3 + 1] = (uint)mesh.Faces[i].Indices[1];
                    index[i * 3 + 2] = (uint)mesh.Faces[i].Indices[2];
                }
                if (!mesh.HasNormals)
                {
                    throw new Exception("模型文件" + path + "不包含法向信息");
                }
                for (int i = 0; i < mesh.Normals.Count; i++)
                {
                    normal[i * 3] = mesh.Normals[i].X;
                    normal[i * 3 + 1] = mesh.Normals[i].Y;
                    normal[i * 3 + 2] = mesh.Normals[i].Z;
                }
                return true;
            }
            position = null;
            index = null;
            normal = null;
            return false;
        }
    }
}
